window.onload=function (){
    var info= getQuery("errorInfo");
    if (info=="noland"){
        $("errorDiv").innerHTML="对不起，请先登录"
    }
    findAll(1)
}


function findAll(pageNO){
    axios.get("/room/findByItem",{
        params:{
            pageNO,
            roomAddress:$("nameTxt").value,
            roomStatus:$("statusSelect").value,
            isLive:$("liveSelect").value,
            roomType:$("typeSelect").value
        }
    }).then(resp=>{
        var info =resp.data;
        str="";
        info.list.forEach(n=>{
            str+=`<tr><td>${n.roomAddress}</td><td>${n.maxNum}</td><td>${n.nowNum}</td><td>${n.roomStatus}</td><td>${n.roomType}</td><td>`
            str+=`<input type="button" value="删除" onclick="delRoom(${n.roomId})"><input type="button" value="查看房间" onclick="findById(${n.roomId})"><input type="button" value="查看设施" onclick="findFac(${n.roomId})">`

            str+=`</td></tr>`
        })
        $("data").innerHTML=str;
        var cutStr = "";
        for(var i=1;i<=info.totalPage;i++){
            cutStr += `<a href="javascript:findAll(${i})" style="margin-left: 10px;text-decoration: none">${i}</a>`
        }

        $("cutDiv").innerHTML = cutStr;
    })
}

function addRoom(){
    axios.get("/room/addRoom",{
        params: {
            roomAddress: $("roomName").value,
            maxNum:$("num").value,
            roomType:$("roomSelect").value
        }
    }).then(resp=>{
        var info =resp.data;
        if (info=="ok"){
            alert("添加成功")
            exit()
            findAll(1);
        }
    })
}

function delRoom(roomId){
    axios.get("/room/delRoom",{
        params:{
            roomId
        }
    }).then(resp=>{
        var info =resp.data;
        if (info=="ok"){
            alert("删除成功")
            findAll(1);
        }
    })
}

function findById(roomId){
    $("roomDiv").style.visibility="visible"
    axios.get("/room/findById",{
        params:{
            roomId
        }
    }).then(resp=>{
        var info =resp.data;
        $("addressSpan").innerHTML=info.roomAddress;
        $("maxNumSpan").innerHTML=info.maxNum;
        $("statusSpan").innerHTML=info.roomStatus;
        $("nowNumSpan").innerHTML=info.nowNum;
        $("typeSpan").innerHTML=info.roomType;

        var str1="";
        info.stuList.forEach(n=>{

            str1+=`<tr><td>${n.stuName}</td><td>${n.stuClass.className}</td><td>${n.stuPhone}</td></tr>`

        })
        $("stuData").innerHTML=str1;
    })
}
function showAddDiv(){
    $("addDiv").style.visibility="visible";
}

function exit(){
    $("addDiv").style.visibility="hidden";
    $("roomDiv").style.visibility="hidden";
    $("facDiv").style.visibility="hidden";
}

function findFac(roomId){
    $("facDiv").style.visibility="visible"
    axios.get("/fac/findFacById",{
        params:{
            roomId
        }
    }).then(resp=>{
        var info =resp.data;
        let str2="";
        $("facBtn").value = roomId;
        info.forEach(n=>{
            str2+=`<tr><td>${n.facDescribe}</td><td>${n.facDate}</td><td>${n.facStatus}</td><td>`
            if (n.facStatus=="未解决"){
                str2+=`<input type="button" value="处理设施" onclick="changeStatus(${n.facId},${n.facRoom.roomId})">`
            }
            str2+=`</td></tr>`
        })
        $("facData").innerHTML=str2;
    })
}

function addFac(){
    axios.get("/fac/addFac",{
        params:{
            roomId:$("facBtn").value,
            facDescribe:$("textarea").value
        }
    }).then(resp=>{
        var info=resp.data;
        if (info=="ok"){
            alert("添加成功")
            findFac($("facBtn").value);
        }
    })
}

function changeStatus(facId,roomId){
    axios.get("/fac/solveFac",{
        params:{
            facId,
            roomId
        }
    }).then(resp =>{
        findFac(roomId);
    })
}
